package com.lil.qmui.view;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lil.qmui.R;


/**
 * 自定义titlebar
 *
 * @author zhazb
 */
public class TitleBarView extends RelativeLayout {

    public TitleBarView(Context context) {
        super(context);
        initView(context);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private RelativeLayout title;
    private TextView leftContent, leftContent2, leftContent3, centerContent, rightContent;
    private ImageView right1View, right2View;
    private ImageView cBox;

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.common_titlebar, this);
        title = (RelativeLayout) findViewById(R.id.common_titlebar);

        leftContent = (TextView) findViewById(R.id.common_titlebar_tv_left);
        leftContent2 = (TextView) findViewById(R.id.common_titlebar_tv_left_two);
        leftContent3 = (TextView) findViewById(R.id.common_titlebar_tv_left_three);
        centerContent = (TextView) findViewById(R.id.common_titlebar_tv_center);
        rightContent = (TextView) findViewById(R.id.common_titlebar_tv_right);

        right1View = (ImageView) findViewById(R.id.common_titlebar_img_right_one);
        right2View = (ImageView) findViewById(R.id.common_titlebar_img_right_two);

        cBox = (ImageView) findViewById(R.id.common_titlebar_checkBox);

    }

    /**
     * 设置左侧view是否可点击
     *
     * @param enable
     */
    public void setRightViewEnable(boolean enable) {
        rightContent.setEnabled(enable);
    }

    /**
     * 设置左侧返回按钮是否可见，默认可见
     *
     * @param isShown
     */
    public void setLeftViewVisible(boolean isShown) {
        leftContent.setVisibility(isShown ? View.VISIBLE : View.GONE);
    }


    /**
     * 设置checkbox 致空则影藏
     *
     * @param param
     */
    public void setCheckBox(String param) {
        if (param != null) {
            cBox.setBackgroundResource(R.drawable.transport);
            cBox.setVisibility(View.VISIBLE);
            rightContent.setText(param);
            // tv_right.setVisibility(View.GONE);
        } else {
            // tv_right.setVisibility(View.VISIBLE);
            cBox.setVisibility(View.GONE);
        }
    }

    /**
     * 设置checkbox 致空则影藏 若 resId = -1 则隐藏checkBox,显示right textview
     *
     * @param resId
     * @param param rightview显示文字，若resId ！= -1时才有效果
     */
    public void setCheckBox(int resId, String param) {
        if (resId != -1) {
            cBox.setVisibility(View.VISIBLE);
            rightContent.setVisibility(View.GONE);
        } else {
            rightContent.setVisibility(View.VISIBLE);
            rightContent.setText(param);
            cBox.setVisibility(View.GONE);
        }
    }

    public void setCheckBoxChecked(boolean isChecked) {
        if (isChecked) {
            cBox.setBackgroundResource(R.drawable.single_selected_pressed);
        } else {
            cBox.setBackgroundResource(R.drawable.transport);
        }
    }


    /**
     * 设置popwindow
     *
     * @param pop
     * @param title
     */
    public void setPopWindow(PopupWindow pop, TitleBarView title) {
        pop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        pop.showAsDropDown(title, title.getWidth() / 2, 0);
        // pop.setAnimationStyle(R.style.popwin_anim_style);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.update();
    }

    /**
     * 设置titlebar 背景
     *
     * @param resId
     */
    public void setTitleBackgroundResource(int resId) {
        title.setBackgroundResource(resId);
    }

    public void setTitleBackgroundColor(int resId) {
        title.setBackgroundColor(resId);
    }


    public ImageView getRightView() {
        return right1View;
    }


    /**
     * 设置左边文字
     */
    public void setLeftViewContont(String content) {
        leftContent.setText(content);
    }

    /**
     * 设置左边文字颜色
     */
    public void setLeftViewTextColor(int textColor) {
        leftContent.setTextColor(textColor);
    }

    /**
     * 设置左侧view是X
     */
    public void setLeftClosedView() {
        leftContent.setVisibility(View.GONE);
        leftContent2.setVisibility(View.VISIBLE);
    }

    /**
     * 设置左侧view的图片
     *
     * @param rID
     */
    public void setLeftViewBackground(int rID) {
        Drawable drawable = getResources().getDrawable(rID);
        //Rect bounds = new Rect(0, 0, getWidth(), getHeight());
        //drawable.setBounds(bounds);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        leftContent.setCompoundDrawables(drawable, null, null, null);
    }

    public void setLeftViewOnClickListener(OnClickListener listener) {
        leftContent.setOnClickListener(listener);
        leftContent2.setOnClickListener(listener);
    }


    /**
     * 设置中间文字
     */
    public void setCenterViewContent(String content) {
        centerContent.setText(content);
    }


    /**
     * 设置右边文字
     */
    public void setRightViewContent(String content) {
        rightContent.setText(content);
    }

    /**
     * 右侧按钮监听事件
     *
     * @param listener
     */
    public void setRightViewOnClickListener(OnClickListener listener) {
        rightContent.setOnClickListener(listener);
    }

    /**
     * 又测按钮是否可见
     *
     * @param visible
     */
    public void setRightViewVisible(boolean visible) {
        if (visible) {
            rightContent.setVisibility(View.VISIBLE);
        } else {
            rightContent.setVisibility(View.GONE);
        }
    }

    /**
     * 设置右边1图片资源
     *
     * @param resId (R.drawable.xx)
     */
    public void setImgRightOneResource(int resId) {
        right1View.setImageResource(resId);
        right1View.setVisibility(View.VISIBLE);
    }

    public void setImgRightOneOnclickListener(OnClickListener listener) {
        right1View.setOnClickListener(listener);

    }

    public void setImgRightOneColorFilter(int color) {
        right1View.setColorFilter(color);
    }

    /**
     * 设置右边2图片资源
     *
     * @param resId
     */
    public void setImgRightTwoResource(int resId) {
        right2View.setImageResource(resId);
        right2View.setVisibility(View.VISIBLE);
    }

    public void setImgRightTwoOnclickListener(OnClickListener listener) {
        right2View.setOnClickListener(listener);
    }

    public void setCheckBoxOnclickListener(OnClickListener listener) {
        cBox.setOnClickListener(listener);
    }

    public void setLeftThirdViewContent(String content, int textcolor) {
        leftContent3.setTextColor(textcolor);
        leftContent3.setText(null == content ? "" : content);
        leftContent3.setVisibility(VISIBLE);
    }

    public void setLeftThirdViewContentVisible(boolean visible) {
        if (visible) {
            leftContent3.setVisibility(VISIBLE);
        } else {
            leftContent3.setVisibility(GONE);
        }
    }

    public void setLeftThirdViewOnClickListener(OnClickListener listener) {
        if (null != listener) {
            leftContent3.setOnClickListener(listener);
        }
    }

    public ImageView getRight1View() {
        return right1View;
    }

    //修改标题栏背景颜色
    public void setLayoutBackground(int background) {
        title.setBackgroundColor(background);
    }

    public TextView getCenterContent() {
        return centerContent;
    }

    public void setTitleVisibility(boolean visibility) {
        if (visibility) {
            title.setVisibility(VISIBLE);
        } else {
            title.setVisibility(GONE);
        }
    }
}
